package com.desktop.click.tool.frame;

import com.desktop.click.tool.entity.RobotClickProperties;
import com.desktop.click.tool.listener.KeyboardListener;
import com.desktop.click.tool.util.RobotUtil;
import com.desktop.click.tool.util.SerializableUtil;
import com.desktop.click.tool.util.TableDataUtil;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.plaf.ButtonUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Vector;


/**
 * 程序主窗口
 */
public class Main extends JFrame {
    private Vector<RobotClickProperties> properties = new Vector<>();
    private Vector tableData;
    private Add add;
    private JButton jButton1;
    private JButton jButton2;
    private JButton jButton3;
    private JButton jButton4;
    private JScrollPane jScrollPane;
    private JTable table;
    private Main main;
    private KeyboardListener keyboardListener;

    public void reloadTable() {
        TableDataUtil.clear();
        tableData = TableDataUtil.getTableData(properties);
        table.updateUI();

    }

    {
        properties = SerializableUtil.getDataByLocalFile();
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public Main() throws IOException {
        setIconImage(ImageIO.read(new File("ico/main.jpeg")));
        main = this;
        //设置右上角关闭
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置标题
        setTitle("桌面点击辅助工具");
        //布局
        setLayout(null);


        jButton1 = new JButton();
        jButton1.setText("启动辅助器");
        jButton1.setBounds(10, 10, 100, 30);

        jButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (properties.size() == 0) {
                    JOptionPane.showMessageDialog(null, "当前无配置，无法运行", "消息提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                if (RobotUtil.getRunStatus()) {
                    JOptionPane.showMessageDialog(null, "请先停止，才可再次运行", "消息提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }

                if (!RobotUtil.getRunStatus()) {
                    RobotUtil.robotRun(properties);
                }

            }
        });

        jButton2 = new JButton();
        jButton2.setText("停止辅助器");
        jButton2.setBounds(130, 10, 100, 30);
        jButton2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (RobotUtil.getRunStatus()) {
                    RobotUtil.stop();
                }
                System.out.println("jbt2");
            }
        });

        jButton3 = new JButton();
        jButton3.setText("配置辅助参数");
        jButton3.setBounds(365, 10, 100, 30);
        jButton3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (add != null) {
                    add.setVisible(true);
                } else {
                    try {
                        add = new Add(properties, main);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    keyboardListener.setAdd(add);
                }
            }
        });

        jButton4 = new JButton();
        jButton4.setText("清空参数");
        jButton4.setBounds(490, 10, 100, 30);
        jButton4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                properties.clear();
                SerializableUtil.saveLocalFile(properties);
                reloadTable();
            }
        });


        jScrollPane = new JScrollPane();
        jScrollPane.setBounds(10, 50, 580, 400);
        tableData = TableDataUtil.getTableData(properties);
        table = new JTable(tableData, TableDataUtil.getTableTitle());
        table.setEnabled(false);
        table.getTableHeader();
        jScrollPane.getViewport().add(table);

        add(jButton1);
        add(jButton2);
        add(jButton3);
        add(jButton4);
        add(jScrollPane);
        //设置可见性
        setVisible(true);
        //全局键盘监听
        keyboardListener = new KeyboardListener();
        keyboardListener.start();

        setBounds(660, 240, 610, 500);

    }


}
